/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include <stb_image.h>
#include <stb_image_resize.h>

#include "底层绘图/S_纹理.h"
#include "底层绘图/底层绘图框架.h"



void f_tex_加载图像文件(S_纹理添加列队& 纹理) {
	uvec3 维度 = { 0,0, 1 };
	纹理.m_内存数据 = stbi_load(纹理.m_加载路径.c_str(),
								(int32*)&纹理.m_新大小.x,
								(int32*)&纹理.m_新大小.y,
								(int32*)&纹理.m_新大小.z,
								4);

	纹理.m_新大小.z = 1;
}

void f_tex_加载图像到GPU(S_纹理添加列队& 列队, S_结构对象指针 命令缓存) {
	f_tex_修改大小(列队.m_纹理对象, 列队.m_新大小);
	f_tex_填充纹理数据(列队.m_纹理对象, (const uint8*)列队.m_内存数据, sizeof(ubvec4), 列队.m_纹理对象->m_Size, 命令缓存);
}

void f_tex_从文件填充纹理(S_纹理* tex, std::string path, S_结构对象指针 命令缓存) {
	uvec3 维度 = { 0,0, 1 };

	stbi_uc* data_p = stbi_load(path.c_str(), (int32*)&维度.x, (int32*)&维度.y, (int32*)&维度.z, 4);
	switch (维度.z) {
	case 1: {
		break;
	}
	case 2: {
		break;
	}
	case 3: {
		break;
	}
	case 4: {
		break;
	}
	}

	if(!维度.x || !维度.y) return;
	维度.z = 1;
	//lodepng_decode_file((uint8**)&data_p, &维度.x, &维度.y, path.c_str(), LodePNGColorType::LCT_RGBA, 8);
	//std::vector<ubvec4> 变换像素(维度.x* 维度.y);
	//uint8 *变换像素 = nullptr;
	//stbir_resize_uint8(data_p, 维度.x, 维度.y, 0, (uint8*)变换像素.data(), 维度.x, 维度.y, 0, 4);
	

	f_tex_修改大小(tex, 维度);
	f_tex_填充纹理数据(tex, (const uint8*)data_p, sizeof(ubvec4), 维度, 命令缓存);

	stbi_image_free(data_p);
	//free(data_p);
}

void f_tex_从文件填充纹理(S_纹理* tex, std::string path, S_子集绘图命令* 绘图命令) {
	uvec3 维度 = { 0,0, 1 };
	stbi_uc* data_p = stbi_load(path.c_str(), (int32*)&维度.x, (int32*)&维度.y, (int32*)&维度.z, 4);

	if (!维度.x || !维度.y) return;
	维度.z = 1;


	f_tex_修改大小(tex, 维度);
	f_tex_填充纹理数据(tex, (const uint8*)data_p, sizeof(ubvec4), 维度, S_绘图框架::g_资源加载管道->m_命令缓存[0]);

	stbi_image_free(data_p);

}

